package com.xiaoying.server.publish.impl;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.xiaoying.server.publish.Handler;
import com.xiaoying.server.publish.entity.FileInfo;
import com.xiaoying.server.publish.entity.UploadResult;
import com.xiaoying.server.publish.service.UploadResultService;
import com.xiaoying.server.publish.task.BackupUploadInfoDealTask;
import com.xiaoying.server.publish.task.ResultDealTask;
import com.xiaoying.server.publish.utils.SpringContext;
import com.xiaoying.server.publish.utils.SpringUtil;

@Controller("responseHandler")
public class ResponseHandler implements Handler,Runnable,InitializingBean {
    
    private static Log log = LogFactory.getFactory().getInstance(ResponseHandler.class);
    private int maxThread=5;
    private BlockingQueue<UploadResult> resQueue;
    @Resource
    private Executor executor;
    
//    private Executor executor2;
    
	@Resource(name="uploadResultService")
	private UploadResultService uploadResultService;
	
	private static UploadResultService uploadResultServiceStatic;
	
	public ResponseHandler(){
		
	}
    
    @Override
    public void handle(BlockingQueue<FileInfo> reqQueue,
            BlockingQueue<UploadResult> resQueue) {
        this.resQueue = resQueue;
        Thread proc = new Thread(this);
        proc.start();
    }

    @Override
    public void init(Configuration config) throws Exception {
        if (log.isDebugEnabled())
        	log.debug("初始化上传结果处理程序...");
        maxThread = config.getInt("ResponseHandler.maxThread",40);
        executor = Executors.newFixedThreadPool(maxThread);        
    }
    

	@Override
	public void afterPropertiesSet() throws Exception {
		
	}
    
    @Override
    public void run() {
        while (true) {
            try {
                if (log.isDebugEnabled())
                    log.debug("ResponseHandler responseQueue.size():"+resQueue.size());        
                UploadResult uploadResult = resQueue.take();
                
//        		ApplicationContext ctx = SpringUtil.getInstance();
//        		UploadResultService uploadResultService =
//        			(UploadResultService)ctx.getBean("uploadResultService");                
                
                ResultDealTask task = new ResultDealTask(resQueue, uploadResult, uploadResultServiceStatic);
                BackupUploadInfoDealTask backupTask = new BackupUploadInfoDealTask(uploadResult);
                executor.execute(task);
                executor.execute(backupTask);
            } catch (InterruptedException e) {
                log.error(e);
            }            
        }
    }

	public UploadResultService getUploadResultService() {
		return uploadResultService;
	}

	public void setUploadResultService(UploadResultService uploadResultService) {
		this.uploadResultService = uploadResultService;
		uploadResultServiceStatic = this.uploadResultService;
	}

	public Executor getExecutor() {
		return executor;
	}

	public void setExecutor(Executor executor) {
		this.executor = executor;
	}

	
	

}
